<?php

namespace App\Backend\Http\Controllers;

use App\Admin;
use App\Order;
use App\Tixian;
use App\User;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;

class CountController extends Controller
{
    //注册统计
    public function register()
    {
        $data = [
            'user' => User::distinct('id')->count('id'),
        ];
        $items =User::select('id',DB::raw('DATE_FORMAT(created_at,"%Y-%m-%d") as day'))
            ->orderBy('created_at', 'desc')->get();
        $data['lists'] = collect($items)->groupBy('day');
        return view('backend::count.register',$data);
    }
    //提现统计
    public function tixian()
    {
        $data = [
            'user' => Tixian::distinct('user_id')->count('user_id'),
            'tixianmoney' => Tixian::sum('tixianmoney'),
            'money' => User::sum('money')/100,
        ];
        $items =Tixian::select('id','user_id','tixianmoney',DB::raw('DATE_FORMAT(created_at,"%Y-%m-%d") as day'))
            ->orderBy('created_at', 'desc')->get();
        $data['lists'] = collect($items)->groupBy('day')->map(function ($item){
            return [
                'tixianmoney' => $item->sum('tixianmoney'),
                'user' => $item->groupBy('user_id')->count()
            ];
        });
        return view('backend::count.tixian',$data);
    }
    //充值统计
    public function pay(){
        $data = [
            //充值交易币成功总人数
            'user' => Order::where('status',1)->where('type',0)->distinct('user_id')->count('user_id'),
            //充值vip总人数
            'vipuser' => Order::where('status',1)->where('type',1)->distinct('user_id')->count('user_id'),
            //累计充值钱数
            'money' => Order::where('status',1)->sum('money')/100,
        ];
        $data['avgmoney'] = intval($data['money']/$data['user']);
        $items =Order::where('status',1)->select('id','user_id','coins','type','money',DB::raw('DATE_FORMAT(created_at,"%Y-%m-%d") as day'))->orderBy('created_at', 'desc')->get()->toArray();
        $data['lists'] = collect($items)->groupBy('day')->map(function ($item){
            $money = $item->sum('money')/100;
            $user = $item->groupBy('user_id')->count();
            return [
                'money' => $money,
                'user' => $user,
                'vipuser' => $item->groupBy('type')->map(function($ite){
                    return $ite->groupBy('user_id')->count();
                })->toArray(),
                'avgmoney' => intval($money/$user),
            ];
        });
        return view('backend::count.pay',$data);
    }

    public function down(){
        dd(11);
    }
}
